package com.mexico.inloancash.http;

import android.os.Environment;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.FileNameMap;
import java.net.URLConnection;
import java.net.URLEncoder;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by ASUS on 2017/8/3.
 */

@SuppressWarnings("WeakerAccess")
public class TrivialUtil {
    public static String guessMimeType(String filename) {
        FileNameMap fileNameMap = URLConnection.getFileNameMap();
        String contentTypeFor = null;
        try {
            contentTypeFor = fileNameMap.getContentTypeFor(URLEncoder.encode(filename, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (contentTypeFor == null) {
            contentTypeFor = "application/octet-stream";
        }
        return contentTypeFor;
    }


    //fun File.guessRequestBody(): RequestBody = RequestBody.create(MediaType.parse(this.name.guessMimeType()), this)

    @NonNull
    public static RequestBody guessRequestBody(File file) {
        return RequestBody.create(MediaType.parse(guessMimeType(file.getName())), file);
    }

    @NonNull
    public static String rootPath(){
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }
}